﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.Sql;

public partial class Boss_EditMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string Dno = Request.QueryString["Dno"];
            fullFillData(Dno);
        }

    }
    private void fullFillData(string Dno)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrBoss"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();

                String sqlString = "select * from Boss_Menu where Dno = @Dno";
                SqlParameter[] parameters = { new SqlParameter("@Dno", Dno) };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataReader dr = cmd.ExecuteReader();
                    dr.Read();

                    DnoTex.Text = dr["Dno"].ToString();
                    DnameTex.Text = dr["Dname"].ToString();
                    DpriceTex.Text = dr["Dprice"].ToString();
                    DesTex.Text = dr["Ddes"].ToString();
                    DstoreTex.Text = dr["Dstore"].ToString();

                    if (dr["Dclass"].ToString() == "饭")
                        ClassRadioList.SelectedValue = "饭";
                    else if (dr["Dclass"].ToString() == "汤")
                        ClassRadioList.SelectedValue = "汤";
                    dr.Close();
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }


    }
    protected void ResetBut_Click(object sender, EventArgs e)
    {
        string Dno = Request.QueryString["Dno"];
        fullFillData(Dno);
    }
    protected void UpdateBut_Click(object sender, EventArgs e)
    {
        string Dno = Request.QueryString["Dno"];
        saveData(Dno);
    }

    private void saveData(string Dno)
    {

        string Dname = DnameTex.Text;
        string Dprice = DpriceTex.Text;
        string Ddes = DesTex.Text;
        string Dclass = ClassRadioList.SelectedValue;
        string Dstore = DstoreTex.Text;

        if (checkData(Dno, Dname, Dprice, Ddes, Dclass, Dstore) == false)
        {
            Response.Write(@"<script>alert('信息填写不符合要求！');</script>");
            return;
        }
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrBoss"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                //String sqlString = "select * from Customer_Account where Aaccount = @username";
                String sqlString = "update Boss_Menu set Dname= @Dname,  Dprice = @Dprice, Ddes = @Ddes, Dclass = @Dclass, Dstore = @Dstore where Dno = @Dno";
                SqlParameter[] parameters = { 
                                                new SqlParameter("@Dno", Dno),
                                                new SqlParameter("@Dname", Dname),
                                                new SqlParameter("@Dprice", Dprice),                                                
                                                new SqlParameter("@Ddes", Ddes),
                                                new SqlParameter("@Dclass", Dclass),
                                                new SqlParameter("@Dstore", Dstore)

                                            };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    int ret = cmd.ExecuteNonQuery();
                    if (ret > 0)
                    {
                        Response.Write(@"<script>alert('修改成功');</script>");
                        Response.AddHeader("Refresh", "0");  //刷新当前页面
                    }
                    else
                    {
                        Response.Write(@"<script>alert('修改失败');</script>");
                    }
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
    private bool checkData(string Dno, string Dname,string  Dprice,string  Ddes,string  Dclass, string Dstore)
    {
        bool ans = true;
        if (Dno.Length > 8)
            ans = false;
        else if (Dname.Length > 20 || Dname.Length == 0)
            ans = false;
        else if (!(Dclass == "饭" || Dclass == "汤"))
            ans = false;
        else if (Ddes.Length>200)
            ans = false;
        return ans;
    }

    protected void DelBut_Click(object sender, EventArgs e)
    {
        bool flag = false;
        //查看订单是否有该员工派送中的，有的话拒绝
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrBoss"].ConnectionString;
        string Dno = Request.QueryString["Dno"];
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();

                String sqlString = "select * from Boss_Choose_Order  where Dno = @Dno and (Ostate = '正在派送' or Ostate = '已接单' or Ostate ='已下订' or Ostate ='申请退单')";
                SqlParameter[] parameters = { new SqlParameter("@Dno", Dno) };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr == null || dr.Read() == false)
                    {
                        flag = true;
                    }
                    dr.Close();
                }
                if (flag == false)
                {//正在被下单，无法删除
                    Response.Write(@"<script>alert('正在处理的订单中存在此菜式，无法删除！');</script>");
                }
                else
                {
                    sqlString = "delete from Boss_Menu where Dno = @Dno";
                    SqlParameter[] parametersnew = {                                             
                                                new SqlParameter("@Dno", Dno)
                                            };
                    using (SqlCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sqlString;
                        cmd.Parameters.AddRange(parametersnew);
                        int ret = cmd.ExecuteNonQuery();
                        if (ret > 0)
                        {
                            Response.Write(@"<script>alert('删除成功');</script>");
                            Response.Redirect("./StaffList.aspx");
                        }
                        else
                        {
                            Response.Write(@"<script>alert('修改失败');</script>");
                        }
                    }
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
    protected void InsBut_Click(object sender, EventArgs e)
    {
        string Dno = DnoTex.Text;
        string Dname = DnameTex.Text;
        string Dprice = DpriceTex.Text;
        string Ddes = DesTex.Text;
        string Dclass = ClassRadioList.SelectedValue;
        string Dstore = DstoreTex.Text;

        if (checkData(Dno, Dname, Dprice, Ddes, Dclass, Dstore) == false)
        {
            Response.Write(@"<script>alert('信息填写不符合要求！');</script>");
            return;
        }
        string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionStrBoss"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            try
            {
                conn.Open();
                //String sqlString = "select * from Customer_Account where Aaccount = @username";
                String sqlString = "insert Boss_Menu (Dno,Dname,Dprice,Ddes,Dclass,Dstore)values (@Dno,@Dname,@Dprice,@Ddes,@Dclass,@Dstore)";
                SqlParameter[] parameters = { 
                                                new SqlParameter("@Dno", Dno),
                                                new SqlParameter("@Dname", Dname),
                                                new SqlParameter("@Dprice", Dprice),                                                
                                                new SqlParameter("@Ddes", Ddes),
                                                new SqlParameter("@Dclass", Dclass),
                                                new SqlParameter("@Dstore", Dstore)

                                            };
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sqlString;
                    cmd.Parameters.AddRange(parameters);
                    int ret = cmd.ExecuteNonQuery();
                    if (ret > 0)
                    {
                        Response.Write(@"<script>alert('修改成功');</script>");
                        Response.AddHeader("Refresh", "0");  //刷新当前页面
                    }
                    else
                    {
                        Response.Write(@"<script>alert('修改失败');</script>");
                    }
                }

            }
            catch (Exception ez)
            {
                Response.Write(@"<b>异常信息:</b>" + ez.Message);
            }
            finally
            {
                conn.Close();
            }
        }
    }
}